<?php

class BookingController extends Zend_Controller_Action
{

    protected $_redirector = null;

    public function newBooking($data)
    {
        $input = array();
        $venue_db = new Application_Model_DbTable_Venue();
        $cost = $venue_db->getVenue($data['vid'])->cost;
        date_default_timezone_set("Asia/Jakarta");

        //get date
        $day = substr($data['date'], 0, 2);
        $month = substr($data['date'], 2, 2);
        $year = substr($data['date'], 4, 4);

        $start_date = "$year/$month/$day";
        $end_date = date("Y/m/d", strtotime($start_date) + (($data['duration'] - 1) * 24 * 60 * 60));

        //check availability
        $bdate_db = new Application_Model_DbTable_BookingDate();
        $occupied = $bdate_db->getBookingDate($data['vid'], $start_date, $end_date);
        if(count($occupied)>0) throw new Exception('Venue is already occupied');

        /*
        $msg .= $start_date.' '.$end_date.' ';

        if (count($occupied)>0){
            $msg .= "Venue is already occupied";
        }else{
            $msg .= "You are allowed to book this venue";
        }

        $error = array('message'=>$msg,'prev'=>'/index');
        $_SESSION['error'] = $error;
        $this->_redirector->gotoUrl('/error/displayerror');
        */

        
        $input['user_id'] = $data['uid'];
        $input['venue_id'] = $data['vid'];
        $input['booking_time'] = date("Y/m/d H:i:s");
        $input['start_date'] = "$year/$month/$day";
        $input['duration'] = $data['duration'];
        $input['total_payment'] = $data['duration'] * $cost;
        $input['organizer'] = $data['organizer'];
        $input['participant'] = $data['participant'];

        // Create booking code
        // Format: uid + 'U' + vid + 'VD' + (day+31) + (month+12) + (year-1000) + hour + minute + second
        $itime = strtotime($input['booking_time']);
        $input['code'] = $data['uid'].'U'.$data['vid'].'VD'.(date("d",$itime)+31).(date("m",$itime)+12).
                (date("Y",$itime)-1000).date("H",$itime).date("i",$itime).date("s",$itime);

        $booking_db = new Application_Model_DbTable_Booking();
        $booking_db->insertBooking($input);
    }

    public function init()
    {
        $this->view->title = "";
        $this->view->bookingctrl = true;

        $this->_redirector = $this->_helper->getHelper("Redirector");

        @session_start();
        if(!isset($_SESSION['user'])){
            $this->_redirector->gotoUrl('/index');
        }
    }

    public function indexAction()
    {
        $this->view->title = "My Bookings";

        // Get cancel action, if exist
        if($this->getRequest()->isPost()){
            $postData = $this->getRequest()->getPost();

            if($postData['act'] == 'cancel'){
                $booking_db = new Application_Model_DbTable_Booking();
                $booking_db->cancelbooking((int)$postData['id'], $postData['reason']);

                $this->_redirector->gotoUrl('/booking/');
            }
        }

        $booking_db = new Application_Model_DbTable_Booking();
        $this->view->bookings = $booking_db->getAllBookings('user_id', (int)$_SESSION['user']['user_id']);
    }

    public function addbookingAction()
    {
        $this->_helper->layout->disableLayout();
        $this->view->title = "Add Booking";

        if($this->getRequest()->isPost()){
            $postData = $this->getRequest()->getPost();
            $form = new Application_Form_Booking();

            if($postData['act'] == "form"){
                $form->uid->setValue($postData['uid']);
                $form->vid->setValue($postData['vid']);

                $this->view->form = $form;
            }
            else if($postData['act'] == "create"){
                if($form->isValid($postData)){
                    try{
                        $this->newBooking($postData);
                    }catch(Exception $ex){
                        $error = array('message'=>$ex->getMessage(),'prev'=>'/index');
                        $_SESSION['error'] = $error;
                        $this->_redirector->gotoUrl('/error/displayerror');
                    }
                    $this->_redirector->gotoUrl('/booking');
                }
                else{
                    $form->populate($postData);
                    $this->view->form = $form;
                }
            }
        }
        else{
            $this->_redirector->gotoUrl('/index');
        }
    }

    public function detailsAction()
    {
        $this->view->title = "Booking Details";
        $bid = $this->getRequest()->getParam('bid',0);

        if($bid > 0){
            $booking_db = new Application_Model_DbTable_Booking();
            $this->view->details = $booking_db->getBooking('booking_id', (int)$bid);
            $this->view->cancel_reason = $booking_db->getCancelReason((int)$bid);
        }
        else{
            $this->_redirector->gotoUrl('/booking/');
        }
    }


}





